package com.itheima.friend;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

public class Friend2MapperTask extends Mapper<LongWritable, Text,Text,Text> {

    //A	I-K-C-B-G-F-H-O-D-
    //B	A-F-J-E-
    //C	A-E-B-H-F-G-K-


    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        if(line!=null && !"".equals(line)){
            String[] split = line.split("\t");
            String[] friends = split[1].split("-"); //A F J E
            Arrays.sort(friends);                         //A F J E
            /*for(String f1:friends){
                for(String f2:friends){
                    if(!f1.equals(f2)){
                        context.write(new Text(f1+"-"+f2),new Text(split[0]));
                    }
                }
            }*/

            for (int i = 0; i < friends.length-1; i++) {
                for (int j = i+1; j < friends.length; j++) {
                    context.write(new Text(friends[i]+"-"+friends[j]),new Text(split[0]));
                }
            }

        }
    }
}
